let app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        btnCheckedColor: "#FCB300",
        //{"name":"白色","btnColor":"","checked":false}
        //颜色数据
        sizeList: [],
        //选择的尺码list
        checkedSizeList: [],
        //长按的数组索引
        longPressIndex: '',
        //action-sheet
        actions: [
            {name: '编辑', color: '#07c160', type: "edit"},
            {name: '删除', color: '#EE0000', type: "del"},
        ],
        actionSheetShow: false,
        //popup
        popupShow: false,
        editPopupShow: false,
        //添加颜色的输入值
        addSizeValue: "",
        editSizeValue: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let checkedSizeList = JSON.parse(options.checkedSizeList)
        console.log(checkedSizeList)
        if (checkedSizeList && checkedSizeList.length > 0) {
            this.setData({
                checkedSizeList: checkedSizeList
            })
        }
        if (app.globalData.setupInfo) {
            this.initSizeList()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    initSizeList: function () {
        let sizes = app.globalData.setupInfo.sizes
        console.log("size=====")
        console.log(sizes)
        let checkedSizeList = this.data.checkedSizeList
        if (sizes && sizes.length > 0) {
            let sizeList = []
            for (let i = 0; i < sizes.length; i++) {
                let size = {}
                let name = sizes[i]
                size.name = name
                //判断是否为选中状态
                if (checkedSizeList.indexOf(name) > -1) {
                    size.btnColor = this.data.btnCheckedColor
                    size.checked = true
                }
                sizeList.push(size)
            }
            this.setData({
                sizeList: sizeList
            })
        }
    },

    sizeChecked: function (e) {
        let index = e.currentTarget.id
        let sizeList = this.data.sizeList
        let size = sizeList[index]
        size.btnColor = size.btnColor ? "" : this.data.btnCheckedColor
        size.checked = size.checked ? false : true
        this.setData({
            sizeList: sizeList
        })
    },
    sizeLongPress: function (e) {
        let index = e.currentTarget.id
        this.setData({
            actionSheetShow: true,
            longPressIndex: index
        })
    },
    handleConfirm: function () {
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        //过滤出选中的
        let checkedSizeList = this.data.sizeList.filter(t => t.checked).map(t => t.name)
        prevPage.setData({
            checkedSizes: checkedSizeList
        })
        wx.navigateBack({
            delta: 1,
        })
    },
    addSize: function () {
        this.setData({popupShow: true});
    },
    actionSelect: function (e) {
        let sizeList = this.data.sizeList
        let longPressIndex = this.data.longPressIndex
        let type = e.detail.type
        if (type == 'edit') {
            this.setData({editPopupShow: true});
        } else if (type == 'del') {
            sizeList.splice(longPressIndex, 1)
            this.setData({
                sizeList: sizeList
            })

            if (app.globalData.setupInfo && app.globalData.setupInfo.sizes) {
                let sizes = app.globalData.setupInfo.sizes
                sizes.splice(longPressIndex, 1)
                //更新用户设置
                this.handleSetup()
            }
        }
    },
    actionCancel() {
        this.setData({actionSheetShow: false});
    },
    actionClose: function () {
        this.setData({actionSheetShow: false});
    },
    cancelAddSize: function () {
        this.setData({popupShow: false});
    },
    cancelEditSize: function () {
        this.setData({editPopupShow: false});
    },
    confirmAddSize: function () {
        let sizes = []
        if (app.globalData.setupInfo && app.globalData.setupInfo.sizes) {
            sizes = app.globalData.setupInfo.sizes
            sizes.push(this.data.addSizeValue)
        } else {
            sizes.push(this.data.addSizeValue)
        }
        app.globalData.setupInfo.sizes = sizes

        let sizeList = this.data.sizeList
        //添加的尺码默认选中
        let sizeObj = {}
        sizeObj.name = this.data.addSizeValue
        sizeObj.btnColor = this.data.btnCheckedColor
        sizeObj.checked = true
        sizeList.push(sizeObj)
        this.setData({
            sizeList: sizeList,
            addSizeValue: ''
        })
        //更新用户设置
        this.handleSetup()
    },
    confirmEditSize: function () {
        let sizeList = this.data.sizeList
        let longPressIndex = this.data.longPressIndex
        let sizeObj = sizeList[longPressIndex]
        sizeObj.name = this.data.editSizeValue
        this.setData({
            sizeList: sizeList,
            editSizeValue: ""
        })

        if (app.globalData.setupInfo && app.globalData.setupInfo.sizes) {
            let sizes = app.globalData.setupInfo.sizes
            sizes[longPressIndex] = this.data.editSizeValue
            //更新用户设置
            this.handleSetup()
        }
    },
    handleSetup() {
        let _this = this
        let data = {}
        data.sizes = app.globalData.setupInfo.sizes
        app.formPost('/app/boss/setup/edit', data).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                app.message(res.message, 'success')
                this.setData({popupShow: false});
                this.setData({editPopupShow: false});
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    }
})



